/**
 * Copyright(c) Live2D Inc. All rights reserved.
 *
 * Use of this source code is governed by the Live2D Open Software license
 * that can be found at https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html.
 */

#include "CubismDebug.hpp"

#include "CubismFramework.hpp"

#include <stdarg.h>
#include <stdio.h>

//------------ LIVE2D NAMESPACE ------------
namespace Live2D
{
    namespace Cubism
    {
        namespace Framework
        {
            namespace Utils
            {
                void CubismDebug::Print(CubismFramework::Option::LogLevel logLevel, const QString &format, ...)
                {
                    // オプションで設定されたログ出力レベルを下回る場合はログに出さない
                    if (logLevel < CubismFramework::GetLoggingLevel())
                        return;

                    const Core::csmLogFunction logPrint = CubismFramework::CoreLogFunction;

                    if (!logPrint)
                        return;

                    char buffer[256];
                    va_list va;
                    va_start(va, format);
#ifdef _WINDOWS
                    vsnprintf_s(buffer, sizeof(buffer), _TRUNCATE, format, va);
#else
                    vsnprintf(buffer, sizeof(buffer), format.toStdString().c_str(), va);
#endif
                    va_end(va);

                    logPrint(buffer);
                }

                void CubismDebug::DumpBytes(CubismFramework::Option::LogLevel logLevel, const csmUint8 *data, int length)
                {
                    for (int i = 0; i < length; i++)
                    {
                        if (i % 16 == 0 && i > 0)
                            Print(logLevel, "\n");
                        else if (i % 8 == 0 && i > 0)
                            Print(logLevel, "  ");
                        Print(logLevel, "%02X ", (data[i] & 0xFF));
                    }

                    Print(logLevel, "\n");
                }

            } // namespace Utils
        }     // namespace Framework
    }         // namespace Cubism
} // namespace Live2D

//------------ LIVE2D NAMESPACE ------------
